<?php
declare (strict_types = 1);

namespace app\api\controller\User;

use app\BaseController;
use app\common\model\User\Address as AddressModel;
use app\common\model\User\User as UserModel;
use app\common\model\Region as RegionModel;
use app\common\validate\User\Address as AddressValidate;

class Address extends BaseController
{
   
    public function index()
    {
        $userid = $this->request->param('userid');

        $AddressList = AddressModel::where(['userid' => $userid])->select();

        if(!$AddressList) {
            $this->error('暂无收货地址');
        }

        $this->success('查询成功',null,$AddressList);
    }

    public function edit() {
        $params = $this->request->param();

        $address = AddressModel::find($params['addrid']);

        if(!$address) {
            $this->error('该收货地址不存在，请重新选择');
        }

        $User = UserModel::find($params['userid']);

        if(!$User) {
            $this->error('用户不存在');
        }

        $data = [
            'userid' => $params['userid'],
            'consignee' => $params['consignee'],
            'mobile' => $params['mobile'],
            'address' => $params['address']
        ];

        if($params['status'] ==1) {
            AddressModel::where(['userid' => $params['userid']])->updata(['status' => 0]);

            $data['status'] = 1;
        }

        if($params['code']) {
            $parentpath = RegionModel::where(['code' => $params['code']])->value('parentpath');

            [$province,$city,$district] = explode(',',$parentpath);
                
            $data['province'] = $province;

            $data['city'] = $city;

            $data['district'] = $district;
        }

        $result = AddressModel::update($data,['id' => $params['addrid']]);

        if($result !== FALSE) {
            $this->success('修改收货地址成功');
        }else {
            $this->error('修改收货地址失败');
        }
    }

    public function add() {
        
        try {
            $params = $this->request->param();

            $User = UserModel::find($params['userid']);

            if(!$User) {
                throw new \Exception("用户不存在");
                
            }

            $data = [
                'consignee' => $params['consignee'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
                'userid' => $params['userid']
            ];

            if(isset($params['status']) && $params['status'] === 1) {
                AddressModel::update(['status' => 0],['userid' => $params['userid']]);

                $data['status'] = 1;
            }

            if(isset($params['code'])) {
                $parentpath = RegionModel::where(['code' => $params['code']])->value('parentpath');

                if(!$parentpath) {
                    throw new \Exception('所选地区不存在');
                }

                [$province,$city,$district] = explode(',',$parentpath);
                
                $data['province'] = $province;

                $data['city'] = $city;

                $data['district'] = $district;
            }

            validate(AddressValidate::class)->check($data);

            $result = AddressModel::create($data);
            
            if($result === FALSE) {
                throw new \Exception('新增收货地址失败');
            }
        } catch (ValidateException $e) {

            $this->error($e->getError());
        } catch(\Exception $error) {

            $this->error($error->getMessage());
        }

        $this->success('新增收货地址成功');

       
    }

    public function select() {
        $userid = $this->request->param('userid');

        $id = $this->request->param('id');

        $User = UserModel::find($userid);

        if(!$User) {
            $this->error('用户不存在');
        }

        $address = AddressModel::find($id);

        if(!$address) {
            $this->error('所选收货地址不存在');
        }

        AddressModel::update(['status' => 0],['userid' => $userid]);

        $result = AddressModel::update(['status' => 1],['id' => $id]);

        if($result !== FALSE) {
            $this->success('设置默认收货地址成功');
        }else {
            $this->error('设置默认收货地址失败');
        }
        
    }

    public function info() {
        $addrid = $this->request->param('addrid');

        $address = AddressModel::find($addrid);

        if(!$address) {
            $this->error('所选收货地址不存在');
        }

        $this->success('查询成功',null,$address);
    }

    public function order() {
        $userid = $this->request->param('userid');

        $User = UserModel::find($userid);

        if(!$User) {
            $this->error('用户不存在');
        }

        $address = AddressModel::where(['userid' => $userid,'status' => 1])->find();

        print_r($address);
        exit;

        if($address) {
            $this->success('查询成功',null,$address);
        }else {
            $this->error('暂无默认地址');
        }
    }
    
}
